package cn.jiyun.web.controller;

import cn.jiyun.dao.domain.Product;
import cn.jiyun.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("productList")
    public String getproducts(Product product, Model model,
                              @RequestParam(defaultValue = "1",required = false) Integer pageNum,
                              @RequestParam(defaultValue = "3",required = false)Integer limit){
        PageInfo info = productService.getproducts(product, pageNum, limit);
        model.addAttribute("pageInfo",info);
        return  "show";
    }

    @RequestMapping("addpage")
    public String resuletadd(){
        return  "add";
    }

    @RequestMapping("addproduct")
    public String add(Product product){
        productService.addproduct(product);
        return  "redirect:productList";
    }

    @RequestMapping("updateProduct")
    public String updateProduct(Product product){
        productService.updateProduct(product);
        return  "redirect:productList";
    }
    //删除
    @RequestMapping("deleteDept")
    public String deleteProducts(Integer[] id){
        productService.deleteProducts(id);
        return  "redirect:productList";
    }
    //回显
    @RequestMapping("findById")
    public String findById(Integer id,Model model){
       Product product = productService.findByid(id);
       model.addAttribute("product",product);
       return "update";
    }

    //验重
    @RequestMapping("ajax")
    @ResponseBody
    public String ajax(String name){
        List<Product> list = productService.findbyname(name);
        if(list.size()>0){
            return "no";
        }else {
            return "ok";
        }
    }


}
